# @shuaige : 陈世玉
# @name :05-豆瓣电影的前十页.py
# @time :2024/9/23 下午4:20
import urllib.parse
import urllib.request
import json
def create_resqust(i) :
    basre_url="https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&"
    data={
        "start":(i-1)*20,
        "limit":20
    }
    data=urllib.parse.urlencode(data)
    url=basre_url+data
    headers={
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
    }
    request=urllib.request.Request(url=url,headers=headers)
    return request
def get_content(request):
    response=urllib.request.urlopen(request)
    content=response.read().decode("utf-8")
    return content
def download(content):
    fp=open("./movie.json","a",encoding="utf-8")
    fp.write(content)
    fp.close()
def show_title() :
    data=json.loads(content)
    for item in data:
        fp1=open("./movieTitle.txt","a",encoding="utf-8")
        fp1.write(item["title"])
        fp1.write("\n")
        fp1.close()

start_page=int(input('请输入起始页码:'))
end_page=int(input('请输入结束页码:'))
for i in range(start_page,end_page+1):
    print("正在爬取第%d页"%i)
    request=create_resqust(i)
    content=get_content(request)
    show_title()









# url="https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=0&limit=20"
# headers={
#     "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
# }
#
